package com.itheima.bos.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.Standard;
import com.itheima.bos.service.StandardService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	private Standard standard = new Standard();
	@Override
	public Standard getModel() {
		return standard;
	}
	//注入service对象
	@Autowired
	private StandardService stanService;
	
	@Action(value="standard_save",results={@Result(name="success",type="redirect",
			location="/pages/base/standard.html")})
	public String save(){
		System.out.println("添加收派标准。。。。");
		stanService.save(standard);
		return SUCCESS;
	}
	
	//属性驱动
	private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	//分页列表查询
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//调用业务层，查询数据
		Pageable pageable = new PageRequest(page-1, rows);//因为是从0开始的，所以要减一
		Page<Standard> pageData = stanService.findPageData(pageable);
		//返回客户数据，需要title和rows
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total",pageData.getTotalElements());
		result.put("rows",pageData.getContent());
		//将map转换为json数据放回
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
